package cn.tedu.oa.sys.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.tedu.oa.common.vo.JsonResult;
import cn.tedu.oa.sys.dao.MenuDao;
import cn.tedu.oa.sys.entity.Menu;
import cn.tedu.oa.sys.entity.User;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-configs.xml")
@FixMethodOrder(MethodSorters.NAME_ASCENDING)/*指定测试方法按字典排序规则执行*/
public class MenuSeviceTset {
	
	static int menuId;
	
	@Autowired
	private MenuService menuService;
	@Autowired
	private MenuDao menuDao;
	
	@Test
	public void add(){
		Menu menu=new Menu();
		menu.setName("网页");
		menu.setUrl("hhhhh");
		menu.setType(1);
		menu.setSort(5);
		menu.setNote("测试");
		menu.setParentId(1);
		menu.setPermission("user:create");
		menu.setAddTime(new Date());
		menu.setUpdateTime(new Date());
		menu.setAddUser("李四u");
		menu.setUpdateUser("李四u");
		menu.setRemark("add");
		menuDao.add(menu);
		menuId=menu.getId();
		menuService.add(menu);
		Menu returnMenu=new Menu();
		
		assertNotNull(returnMenu);
	}
	
	@Test
	public void update() {
		Menu  saveMenu = menuService.get(menuId);
		String savedMenuRemark = saveMenu.getRemark();
		
		Menu menu=new Menu();
		menu.setId(menuId);
		menu.setAddTime(new Date());
		menu.setAddUser("王五");
		menu.setRemark(savedMenuRemark+",update");
		
		menuService.update(menu);
		String updatedMenuRemark = menu.getRemark();
		
	    assertEquals("add,update",updatedMenuRemark);
	}
	@Test
	public void get() {
		Menu menu = menuService.get(22);
		assertNotNull(menu);
	}
	@Test
	public void getMenuByName(){
		Menu menu = menuService.getMenuByName("系统管理");
		assertNotNull(menu);
	}

	@Test
	public void getSubMenusByPreviousMenu(){
		Menu menu = menuService.getMenuByName("系统管理");
		List<Menu> list = menuService.getSubMenusByPreviousMenuId(107);
		assertNotNull(list);
	}
	
	@Test
	public void getAll(){
		List<Menu> list=menuService.getAll();
		assertNotNull(list);
	}
	@Test
	public void getFirstMenus(){
		List<Menu> list=menuService.getFirstMenus();
		assertNotNull(list);
	}

	@Test
	public void z_delete() {
		Menu  saveMenu = menuService.get(menuId);
		String savedMenuRemark = saveMenu.getRemark();       
		menuService.delete(menuId);	
		
		Menu  menu = menuService.get(menuId);
        int isdel = menu.getIsdel();
        assertEquals(1,isdel);
        menu.setUpdateUser("wei");
        menu.setUpdateTime(new Date());
        menu.setRemark(savedMenuRemark + ",delete");
        
        menuService.update(menu);
        
        Menu updatedMenu = menuService.get(menuId);
        String updatedUserRemark = updatedMenu.getRemark();
        assertEquals("add,update,delete",updatedUserRemark);
	}

	@Test
	public void z_deleteByIds(){
		Integer[] is=new Integer[]{34,35,38};
		menuService.deleteByIds(is);
	}
	

}
